<?php

declare(strict_types=1);

namespace App\Controller\playlet;
use App\Controller\BaseController;
use App\Model\Applet;
use App\Model\AppletPlayletRel;
use App\Model\Playlet;
use App\Model\PlayletEpisodes;
use App\Service\queue\DramaService;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Common\Response\SystemCode;
use App\Service\playlet\PlayletEpisodesService;

#[AutoController]
class PlayletEpisodesController  extends BaseController
{

    #[Inject]
    private PlayletEpisodesService $playletEpisodesService;

    #[Inject]
    private DramaService $dramaService;
    /**
     * 剧集列表
     */
    public function Index(RequestInterface $request)
    {

        $data = $this->playletEpisodesService->search($request->query());

        return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$data);


    }
    /**
     * 剧集详情
     */
    public function View(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $playletEpisodesInfo = PlayletEpisodes::query()->select(['id','playlet_id','playlet_number','playlet_number_name','cover','amount','play_url','original_play_url','likes_number','share_number','created_at','updated_at'])->where('id', $id)->first();
            if (empty($playletEpisodesInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧集数不存在');
            }
            return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功', $playletEpisodesInfo);
        }
    }
    /**
     * 剧集创建
     */
    public function Create(RequestInterface $request){

        $result = $this->playletEpisodesService->createOrUpdate($request->post());

        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '创建成功');
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

        }
    }
    /**
     * 剧集删除
     */
    public function Delete(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $playletEpisodesInfo = PlayletEpisodes::query()->where('id', $id)->first();
            if (empty($playletEpisodesInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧剧集不存在');
            }
            $playletEpisodesInfo->deleted_at = date('Y-m-d H:i:s');
            $playletEpisodesInfo->save();
            return $this->response(SystemCode::SYSTEM_SUCCESS, '操作成功');
        }
    }
    /**
     * 剧集更新
     */
    public function Update(int $id,RequestInterface $request){

        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{

            $playletEpisodesInfo = PlayletEpisodes::query()->select(['id','playlet_id','playlet_number','playlet_number_name','cover','amount','play_url','original_play_url'])->where('id', $id)->first();

            if(empty($playletEpisodesInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧集数不存在');
            }
            if($request->getMethod() == 'GET'){
                return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$playletEpisodesInfo);
            }else{
                $result = $this->playletEpisodesService->createOrUpdate($request->post(),true,$playletEpisodesInfo);

                if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                    return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
                }else{
                    return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

                }
            }
        }
    }

    /**
     * 剧集审核
     */
    public function Examine(RequestInterface $request){

        $ids = $request->post('ids');
        $type = $request->post('type') ?? 1;

        if(empty($ids)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{
            $playletEpisodesArray = PlayletEpisodes::query()->whereIn('id', $ids)->get();
            if(empty($playletEpisodesArray)){
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧集数不存在');
            }
            //判断上传的ids数目和搜索出来的数目是否一致
            if(count($ids) != count($playletEpisodesArray)){
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧集数ID存在错误');
            }
            $errorMeg = [];
            $successMeg = [];
            foreach ($playletEpisodesArray as $playletEpisodes){
                $playlet_name = '';
                if(!empty($playletEpisodes->playlet)){
                    $playlet_name = $playletEpisodes->playlet->name;

                    $is_exists = AppletPlayletRel::query()->where(['playlet_id' => $playletEpisodes->playlet->id])->exists();
                    if(!$is_exists){
                        $errorMeg[] = '该集数的短剧没有绑定小程序';
                        continue;
                    }
                }else{
                    $errorMeg[] = '该集数的短剧不存在';
                    continue;
                }



                if($type == 1){

                    //0为无效值；1为审核中；2为最终失败；3为审核通过；4为驳回重填
                    if($playletEpisodes->media_status == 3){
                        $errorMeg[] = $playlet_name.':'.$playletEpisodes->playlet_number_name.'集数已经审核通过了，请勿反复提交审核';
                        continue;
                    }else if($playletEpisodes->media_status == 1){
                        $errorMeg[] = $playlet_name.':'.$playletEpisodes->playlet_number_name.'集数已在审核中，请勿反复提交审核';
                        continue;
                    }
                    //内部使用,1:为未提交审核，2:已提交审核，3:审核通过，4:审核失败
                    if($playletEpisodes->is_upload == 2){
                        $errorMeg[] = $playlet_name.':'.$playletEpisodes->playlet_number_name.'集数已提交排队，请勿反复提交';
                        continue;
                    }
                    if($playletEpisodes->is_upload == 3){
                        $errorMeg[] = $playlet_name.':'.$playletEpisodes->playlet_number_name.'集数已成功上传，请勿反复提交';
                        continue;
                    }
                }else{
                    if($playletEpisodes->is_dy_upload == 2){
                        $errorMeg[] = $playlet_name.':'.$playletEpisodes->playlet_number_name.'集数已提交排队，请勿反复提交';
                        continue;
                    }
                    if($playletEpisodes->is_dy_upload == 3){
                        $errorMeg[] = $playlet_name.':'.$playletEpisodes->playlet_number_name.'集数已成功上传，请勿反复提交';
                        continue;
                    }
                }

                $successMeg[] = $playlet_name.':'.$playletEpisodes->playlet_number_name.'集数提交审核排队成功';
            }
            if(!empty($errorMeg)){
                return $this->response(SystemCode::SYSTEM_ERROR_UPLOAD_EPISODES,'提交审核排队失败',$errorMeg);
            }


            if($type == 1){
                PlayletEpisodes::query()->whereIn('id', $ids)->update(['is_upload' => 2,'task_id' => 0]);
            }else{
                PlayletEpisodes::query()->whereIn('id', $ids)->update(['is_dy_upload' => 2,'open_video_id' => 0]);

            }
            $this->dramaService->vodPullUpolad($ids,$type);
//
            return $this->response(SystemCode::SYSTEM_SUCCESS, '提交审核排队成功',$successMeg);
        }

    }


    public function test(){

        $this->dramaService->test();
    }


}